Imports System.Xml
Imports System.Reflection

Public Class SettingsDialog
    Private xmlDoc As XmlDocument
    Private configFile As String
    Private connstr As String

    Private Sub SettingsDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            xmlDoc = New XmlDocument
            configFile = Application.ExecutablePath + ".config"
            xmlDoc.Load(configFile)

            Dim nodeConnStr As XmlNode = xmlDoc.SelectSingleNode("configuration/connectionStrings/add")
            connstr = nodeConnStr.Attributes("connectionString").Value

            Dim front As Integer = connstr.IndexOf("Data Source=")
            front = front + 12
            Dim back As Integer = connstr.IndexOf(";", front)
            txtDBServer.Text = connstr.Substring(front, back - front)
            front = connstr.IndexOf("Initial Catalog=")
            front = front + 16
            back = connstr.IndexOf(";", front)
            txtDBName.Text = connstr.Substring(front, back - front)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Try
            Dim nodeConnStr As XmlNode = xmlDoc.SelectSingleNode("configuration/connectionStrings/add")
            connstr = String.Concat("Data Source=", txtDBServer.Text, ";Initial Catalog=", txtDBName.Text, ";Integrated Security=True")
            nodeConnStr.Attributes("connectionString").Value = connstr

            xmlDoc.Save(configFile)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    ReadOnly Property ConnectionString() As String
        Get
            Return connstr
        End Get
    End Property
End Class